<?php
apf_require_controller("Abstract");
apf_require_class("Bll_Hr_User");
apf_require_class('Bll_Hr_UserRelation');
apf_require_class('Bll_Hr_Job');
apf_require_class("Bll_Hr_Property");
apf_require_class("Bll_Hr_Department");
apf_require_class("Const_Work");
apf_require_class("Const_Util");
apf_require_class("Portal_Urls");
apf_require_class("Bll_Portal_HomeData");
class Portal_Home_ProfileController extends AbstractController {

    private $bll_user;
    private $error_item;
    private $bll_search_user;
    public function handle_request() {
        $this->bll_user = new Bll_Hr_User();
        $this->bll_search_user = new Bll_Portal_HomeData();
        //临时过度
        $session_user = $this->get_user_info();
        //$temp_user= $this->bll_user->get_user_by_code($session_user['code']);
        //$user_id = $temp_user['user_id'];
        $user_id = $session_user['user_id'];

        $this->bll_user_relation = new Bll_Hr_UserRelation();
        $this->bll_job = new Bll_Hr_Job();
        $this->bll_property = new Bll_Hr_Property();
        $this->bll_department = new Bll_Hr_Department();

        $post_result = true;
        $params = array();
        //post 提交
        if($this->request->is_post_method()){
            $params = $this->request->get_parameters();

            $post_result = $this->_check_post_data($params);
            if($post_result){
                $data = $this->_build_post_data($params);
                $this->bll_user->update_user($user_id, $data);
                $data['office_phone'] = $data['office_phone'].($data['office_phone_ex']?'-'.$data['office_phone_ex']:'');
                unset($data['office_phone_ex']);
                $this->bll_search_user->update_search_user($user_id, $data);
            }
        }

        $user = array();//用户资料
        $derector_direct_yes = array();//直属上级
        $derector_direct_no = array();//虚线上级
        $sub_users = array();//下属

        //用户存在
        $user = $this->bll_user->get_user_by_id($user_id);
        ##任职信息
        $on_work = $this->bll_user_relation->get_job_by_uids($user['user_id'],Const_Work::WORK_MAIN,Const_Util::IS_DELETE_NO);
        if(!empty($on_work)){
            $on_work = $on_work[0];
            //职位
            $job = $this->bll_job->find_by_job_id($on_work['job_id']);
            $user['position_name'] = $job['position_name'].'&nbsp;&nbsp;';
            //职级
            $p_levels = $this->bll_property->get_p_degree();
            $m_levels = $this->bll_property->get_m_degree();
            /*$user['position_name'] .= $user['p_level']?$p_levels[$user['p_level']]['degree']:'';
            $user['position_name'] .= $user['p_level'] && $user['m_level'] ?"-":'';
            $user['position_name'] .= $user['m_level']?$m_levels[$user['m_level']]['degree']:'';
            */
            //城市
            $citys = $this->bll_property->get_city_list();
            $user['bu_name'] = $citys[$user['work_city_id']]['city_name'];
            //事业部
            $depart = $this->bll_department->get_department_by_id($job['department_id']);
            $bus = $this->bll_property->get_business_list();
            $user['bu_name'] .= $bus[$depart['bu_id']]['bu_name'];
            $user['bu_name'] .= $depart['department_name'];
            //直线主管
            $derector_direct_yes = $this->bll_user_relation->get_director_direct_yes($user['user_id'],$on_work['job_id']);
            //虚线主管
            $derector_direct_no = $this->bll_user_relation->get_director_direct_no($user['user_id'],$on_work['job_id']);
        }
        ##下属
        $user['lower_num'] =0;
        $sub_users_relation = $this->bll_user_relation->get_user_sub($user['user_id'], $on_work['job_id'],Const_Work::WORK_MAIN);
        $uids = array();
        if(!empty($sub_users_relation)){
            foreach($sub_users_relation as $relation){
                $uids[] = $relation['user_id'];
            }
        }
        $sub_users = $this->bll_user->get_user_by_ids($uids);
        ##头像
        $user['head_pic_path'] = Portal_Urls::build_user_pic_path($user['user_code'],Const_Img::PIC_BIG);

        if(!$post_result){//提交失败
            $user['edit_english_name'] = $params['english_name'];
            $user['edit_cellphone'] = $params['cellphone'];
            $user['edit_office_phone'] = $params['office_phone'];
            $user['edit_office_phone_ex'] = $params['office_phone_ex'];
        }else{
            $user['edit_english_name'] = $user['english_name'];
            $user['edit_cellphone'] = $user['cellphone'];
            $user['edit_office_phone'] = $user['office_phone'];
            $user['edit_office_phone_ex'] = $user['office_phone_ex'];
        }

        $this->request->set_attribute('user', $user);
        $this->request->set_attribute('sub_users', $sub_users);
        $this->request->set_attribute('derector_direct_yes', $derector_direct_yes);
        $this->request->set_attribute('derector_direct_no', $derector_direct_no);
        $this->request->set_attribute('error_item', $this->error_item);
        return "Portal_Home_Profile";
    }
    private function _build_post_data($params){
        return array(
                'english_name'=>trim($params['english_name']),
                'cellphone'   =>trim($params['cellphone']),
                'office_phone'=>trim($params['office_phone']),
                'office_phone_ex'=>trim($params['office_phone_ex'])
                );
    }
    /**
     * check post data
     * @param  $params
     */
    private function _check_post_data($params){
        $result = true;
        if(trim($params['english_name']) && !preg_match("/[a-zA-Z .]{1,20}/", trim($params['english_name']))){
            $this->error_item['english_name'] = "您的英文名输入不正确";
            $result = false;
        }
        if(trim($params['cellphone']) && !preg_match("/^0?(13|15|18|14)[0-9]{9}$/", trim($params['cellphone']))){
            $this->error_item['cellphone'] = "手机号码输入不正确";
            $result = false;
        }
        if(trim($params['office_phone']) && !preg_match("/^((0[1,2]{1}\d{1}-?\d{8})|(0[3-9]{1}\d{2}-?\d{7,8}))$/", trim($params['office_phone']))){
            $this->error_item['office_phone'] = "您的公司电话格式不正确";
            $result = false;
        }
        if(trim($params['office_phone_ex']) && (!intval($params['office_phone_ex']) || strlen(trim($params['office_phone_ex'])) < 3 || strlen(trim($params['office_phone_ex'])) > 4)){
            $this->error_item['office_phone_ex'] = "您的公司电话分机号不正确";
            $result = false;
        }
        return $result;
    }
}